package com.beagle.njddapp.ui.activity.owner;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.DIrverUpdateBean;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.ui.adapter.NjDirverListAdapter;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.beagle.njddapp.ui.presenter.FindDirverPresenter;
import com.beagle.njddapp.ui.view.FindDirverView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-02 22:31
 * 寻找农机驾驶员
 */
public class FindDirverActivity extends BaseMvpActivity<FindDirverView, FindDirverPresenter> implements FindDirverView, RecyclerViewItemClickLisenter<DIrverOrderBean>, NjDirverListAdapter.DirverOrderCzCallBack {


    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    @BindView(R.id.common_tv_right)
    TextView tvRight;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    @BindView(R.id.ll_empty)
    View empty;
    @BindView(R.id.smartrefreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.prograssBar)
    ProgressBar progressBar;
    private NjDirverListAdapter njDirverListAdapter;
    private List<DIrverOrderBean> orderBeanList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_find_dirver;
    }

    @Override
    public void init() {
        StatusBarUtil.immersive(this, Color.WHITE);
        StatusBarUtil.darkMode(this);
        StatusBarUtil.setPaddingSmart(this,container);
        container.setBackgroundColor(Color.WHITE);
        tvTitle.setText("寻找农机驾驶员");
        tvRight.setText("添加");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        orderBeanList=new ArrayList<>();
        njDirverListAdapter=new NjDirverListAdapter(orderBeanList,this);
        njDirverListAdapter.setDirverOrderCzCallBack(this);
        njDirverListAdapter.setOnItemClickLisenter(this);
        recyclerView.setAdapter(njDirverListAdapter);
        presenter.getList(1000,1);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                presenter.getList(1000,1);
            }
        });
    }

    @Override
    public FindDirverView getMvpView() {
        return this;
    }

    @Override
    public FindDirverPresenter createPresenter() {
        return new FindDirverPresenter();
    }

    @Override
    public void onError(String message) {
        refreshLayout.finishRefresh();
        ToastUtils.showShort(message);
        progressBar.setVisibility(View.GONE);

    }
    @OnClick({R.id.common_back_iv,R.id.common_tv_right})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.common_tv_right:
                startActivityForResult(new Intent(this, AddDirverActivity.class),33);
                break;
        }
    }

    @Override
    public void showList(List<DIrverOrderBean> results) {
        refreshLayout.finishRefresh();
        progressBar.setVisibility(View.GONE);
        if (results!=null&&results.size()>0){
            empty.setVisibility(View.GONE);
            orderBeanList.clear();
            orderBeanList.addAll(results);
            njDirverListAdapter.notifyDataSetChanged();
        }else {
            empty.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void updateSuccess() {
        progressBar.setVisibility(View.GONE);
        ToastUtils.showShort("操作成功");
        presenter.getList(1000,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        presenter.getList(1000,1);
    }

    @Override
    public void onItemClickLisenter(DIrverOrderBean dIrverOrderBean) {
        startActivityForResult(new Intent(this, AddDirverDetailActivity.class).putExtra("data",dIrverOrderBean),307);
    }

    @Override
    public void caozuo(DIrverOrderBean dIrverOrderBean) {
        if (2==dIrverOrderBean.getOrderStatus()){
            progressBar.setVisibility(View.VISIBLE);
            dIrverOrderBean.setOrderStatus(3);
            presenter.updateFindDriverOrder(dIrverOrderBean);
        }else if (5==dIrverOrderBean.getOrderStatus()){
            progressBar.setVisibility(View.VISIBLE);
            dIrverOrderBean.setOrderStatus(6);
            presenter.updateFindDriverOrder(dIrverOrderBean);
        }
    }
}
